package org.wit.ecommerce.util;

/**
 * 
 * @spring.bean id="pageNavigateCalculator"
 * @author brown.hu
 *
 */
public class PageNavigateCalculator {
	public int getPageSize() {
		return pageSize != 0 ? pageSize : 10;
	}

	/**
	 * @spring.property value="${system.pagesize}"
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageNavigateCalculator() {
		currentPage = 1;
	}

	public int getCurrentPage() {
		if (getTotalPages() > 0)
			return currentPage;
		else
			return 0;
	}

	public boolean HasNext() {
		boolean b;
		if (getCurrentPage() * getPageSize() < getTotalRowsAmount())
			b = true;
		else
			b = false;
		return b;
	}

	public boolean HasPrevious() {
		boolean b;
		if (getCurrentPage() - 1 > 0)
			b = true;
		else
			b = false;
		return b;
	}

	public int getTotalRowsAmount() {
		return totalRowsAmount;
	}

	public void setCurrentPage(int i) {
		currentPage = i;
	}

	public void setTotalRowsAmount(int i) {
		totalRowsAmount = i;
	}

	public int getTotalPages() {
		return (getTotalRowsAmount() - 1) / getPageSize() + 1;
	}

	public int getNextPage() {
		int i;
		if (HasNext())
			i = getCurrentPage() * getPageSize();
		else
			i = 0;
		return i;
	}

	public int getPreviousPage() {
		int i;
		if (HasPrevious())
			i = (getCurrentPage() - 2) * getPageSize();
		else
			i = 0;
		return i;
	}

	public int getCurrentPageRows() {
		return (getCurrentPage() - 1) * getPageSize();
	}

	private int totalRowsAmount;

	private int currentPage;
	private int pageSize;
}
